/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.editor.sheet;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import self.amigo.diag.BasicDiagram;
import self.amigo.editor.APropertySheet;
import self.amigo.editor.cmd.PropertiesChange;
import self.amigo.elem.TextView;
import self.awt.layout.VerticalFlowLayout;
import self.gee.IDiagram;
import self.gee.editor.IPropertySheet;

public class DiagramSheet extends APropertySheet implements IPropertySheet {
	
	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
  JTabbedPane         propertySheet = new JTabbedPane();
  BorderLayout        contentPaneLayout = new BorderLayout();
  JPanel              generalTab = new JPanel();
  VerticalFlowLayout  generalPaneLayout = new VerticalFlowLayout();
  JPanel              generalPane = new JPanel();
  JScrollPane         generalScroller = new JScrollPane();
  BorderLayout        generalLayout = new BorderLayout();

  JLabel titleLabel = new JLabel();
  JTextField title = new JTextField();

  JLabel widthLabel = new JLabel();
  JTextField widthField = new JTextField();

  JLabel heightLabel = new JLabel();
  JTextField heightField = new JTextField();

  JLabel lastSavedLabel = new JLabel();
  JCheckBox modified = new JCheckBox();

  JCheckBox italic = new JCheckBox();
  JLabel fontSizeLabel = new JLabel();
  VerticalFlowLayout fontPaneLayout = new VerticalFlowLayout();
  JCheckBox bold = new JCheckBox();
  JLabel fontNameLabel = new JLabel();
  JPanel fontPanel = new JPanel();
  JComboBox fontName = new JComboBox();
  JTextField fontSize = new JTextField();

  private String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
  private IDiagram view;
  private Font origFont;

  public DiagramSheet() {
    try {
      jbInit();
      fontName.setModel( new DefaultComboBoxModel(fontNames) );
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.setLayout( contentPaneLayout );
    titleLabel.setDisplayedMnemonic('T');
    titleLabel.setLabelFor(title);
    title.setFocusAccelerator('T');

    titleLabel.setText("title");
    widthLabel.setText("width");
    widthLabel.setLabelFor(widthField);
    heightLabel.setText("height");
    heightLabel.setLabelFor(heightField);

    lastSavedLabel.setText("last saved: ");
    modified.setEnabled(false);
    modified.setText("modified");
//    modified.setHorizontalTextPosition(SwingConstants.LEADING);

    generalPane.setLayout(generalPaneLayout);
    italic.setText("italic");
    fontSizeLabel.setToolTipText("");
    fontSizeLabel.setText("size");
    bold.setText("bold");
    fontNameLabel.setText("name");
    fontPanel.setLayout(fontPaneLayout);
    fontSize.setColumns(3);
    generalPane.add(lastSavedLabel, null);
    generalPane.add(modified, null);
    generalPane.add(titleLabel, null);
    generalPane.add(title, null);

    generalPane.add(widthLabel, null);
    generalPane.add(widthField, null);
    generalPane.add(heightLabel, null);
    generalPane.add(heightField, null);

    generalPane.add(fontPanel, null);

    generalScroller.getViewport().add( generalPane, null );
    fontPanel.add(fontNameLabel, null);
    fontPanel.add(fontName, null);
    fontPanel.add(fontSizeLabel, null);
    fontPanel.add(fontSize, null);
    fontPanel.add(bold, null);
    fontPanel.add(italic, null);
    EtchedBorder border1 = new EtchedBorder(EtchedBorder.RAISED,Color.white,new Color(134, 134, 134));
    TitledBorder titledBorder1 = new TitledBorder(border1,"Font");
    fontPanel.setBorder( titledBorder1 );
    generalTab.setLayout(generalLayout);
    generalTab.add( generalScroller, BorderLayout.CENTER );

    this.add(propertySheet, BorderLayout.CENTER);
    propertySheet.add(generalTab, "General");
  }

  public void inspect( Object toDisplay ) {
    if ( toDisplay instanceof IDiagram ) {
      view = (IDiagram) toDisplay;
      revert();
    } else
      throw new RuntimeException( "Invalid Graphic element to display:" + toDisplay );
  }

  public void accept() {
    if ( view == null )
      return;

    store.put( BasicDiagram.TITLE_PROP, title.getText() );
    try {
      store.put( BasicDiagram.WIDTH_PROP, new Integer(widthField.getText()) );
    } catch ( Exception err ) {
    	// nothing to do.
    }
    try {
      store.put( BasicDiagram.HEIGHT_PROP, new Integer(heightField.getText()) );
    } catch ( Exception err ) {
    	// nothing to do.
    }

    int fontSz;
    try {
      fontSz = Integer.parseInt( fontSize.getText() );
    } catch ( Exception err ) {
      fontSz = origFont.getSize();
    }
    int attrMask = 0;
    if ( bold.isSelected() )
      attrMask |= Font.BOLD;
    if ( italic.isSelected() )
      attrMask |= Font.ITALIC;
    String fontNm = fontNames[ fontName.getSelectedIndex() ];
    Font newFont = new Font( fontNm, attrMask, fontSz );
    store.put( TextView.FONT_PROP, newFont );

    PropertiesChange cmd = new PropertiesChange( editor, view, store );
    cmd.executeIfRequired();
  }

  public void revert() {
    if ( view == null )
      return;

    store.clear();
    view.getProperties( store );

    title.setText( (String) store.get(BasicDiagram.TITLE_PROP) );
    widthField.setText( "" + store.get(BasicDiagram.WIDTH_PROP) );
    heightField.setText( "" + store.get(BasicDiagram.HEIGHT_PROP) );

    lastSavedLabel.setText( (String) store.get(BasicDiagram.LASTSAVED_PROP) );
    modified.setSelected( ((Boolean)store.get(BasicDiagram.MODIFIED_PROP)).booleanValue() );

    origFont = (Font) store.get( TextView.FONT_PROP );
    fontName.setSelectedItem( origFont.getName() );
    fontSize.setText( "" + origFont.getSize() );
    bold.setSelected( origFont.isBold() );
    italic.setSelected( origFont.isItalic() );
  }

  public Object getObjectUnderInspection() {
    return view;
  }
}
